﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using DAL;
using MODEL;
namespace BLL
{
    public class ServiceRuKu
    {
        public void RuKu(RuKu r)
        {
            AccessHelp access = new AccessHelp();
            Stock s = new Stock();
            s.ClassId = r.ClassId;
            s.DicId = r.DicId;
            s.GoodsName = r.GoodName;
            s.Number = r.Number;
            s.PY = r.PY;
            string rukusql = "insert into ruku(classid,dicid,goodname,goodsnumber,price,createdate,remark,goodspy) values(@classid,@dicid,@goodname,@goodsnumber,@price,@createdate,@remark,@goodspy)";
            OleDbParameter[] rukupms = new OleDbParameter[]
                {
                    new OleDbParameter("@classid",r.ClassId),
                    new OleDbParameter("@dicid",r.DicId),
                    new OleDbParameter("@goodname",r.GoodName),
                    new OleDbParameter("@goodsnumber",r.Number),
                    new OleDbParameter("@price",r.Price),
                    new OleDbParameter("@createdate",r.Date.Date.ToString()),
                    new OleDbParameter("@remark",r.Remark),
                    new OleDbParameter("@goodspy",r.PY)
                };

            int b = new DalStock().IsExistByDicId(s.DicId);
            string stocksql;
            OleDbParameter[] stockpms;
            int beginnum = 0;
            if (b > 0)
            {
                stocksql = "update stock set goodsnumber=goodsnumber+@goodsnumber where dicid=@dicid";
                stockpms = new OleDbParameter[]
                {
                    new OleDbParameter("@goodsnumber",s.Number),
                    new OleDbParameter("@dicid",s.DicId)
                };
                s.Number = s.Number + new BllStock().GetById(b).Number;
                beginnum = new BllStock().GetById(b).Number;

            }
            else
            {
                //stocksql = "insert into stock(goodsnumber,dicid) values(@goodsnumber,@dicid)";
                stocksql = "insert into stock(goodsnumber,dicid,goodsname,classid,goodspy) values(@goodsnumber,@dicid,@goodsname,@classid,@goodspy)";
                stockpms = new OleDbParameter[]
                {
                    new OleDbParameter("@goodsnumber",s.Number),
                    new OleDbParameter("@dicid",s.DicId),
                    new OleDbParameter("@goodsname",s.GoodsName),
                    new OleDbParameter("@classid",s.ClassId),
                    new OleDbParameter("@goodspy",s.PY)
                };
            }
            //历史库存
            int hbid = new BllHistoryStock().IsExistByDate(r.Date);
            if (hbid <= 0)
            {
                new BllHistoryStock().Insert(r.Date);
                //生成历史库存
                hbid = new BllHistoryStock().IsExistByDate(r.Date);
                GenerateHisStock(r.Date);
            }

            int historydetailid = new BllHistoryStockDetail().IsExist(r.DicId, hbid);
            string hissql;
            OleDbParameter[] hispms;
            if (historydetailid > 0)
            {
                hissql = "update historystockdetail set totalin=totalin+@goodsin,goodsnumber=goodsnumber+@goodsin where id=@id";
                hispms = new OleDbParameter[] { new OleDbParameter("@goodsin", r.Number), new OleDbParameter("@id", historydetailid) };
            }
            else
            {
                //s.Number = s.Number + new BllStock().GetById(b).Number;
                hissql = "insert into historystockdetail(historyid,beginnumber,totalin,goodsnumber,dicid,goodsname,classid,goodspy) values(@historyid,@beginnumber,@totalin,@goodsnumber,@dicid,@goodsname,@classid,@goodspy)";
                hispms = new OleDbParameter[]
                {
                    new OleDbParameter("@historyid",hbid),
                    new OleDbParameter("@beginnumber",beginnum),
                    new OleDbParameter("@totalin",r.Number),
                    new OleDbParameter("@goodsnumber",s.Number),
                    new OleDbParameter("@dicid",s.DicId),
                    new OleDbParameter("@goodsname",s.GoodsName),
                    new OleDbParameter("@classid",s.ClassId),
                    new OleDbParameter("@goodspy",s.PY) 
                };
            }
            try
            {
                access.OpenTran();
                access.ExecuteNonQuery(rukusql, rukupms);
                access.ExecuteNonQuery(stocksql, stockpms);
                access.ExecuteNonQuery(hissql, hispms);
                access.CommitTran();
            }
            catch (Exception ex)
            {
                access.TranRollBack();
                throw ex;
            }



        }

        public void ChuKu(ChuKu c)
        {
            AccessHelp access = new AccessHelp();
            Stock s = new Stock();
            string rukusql = "insert into chuku(classid,dicid,goodname,goodsnumber,price,createdate,remark,goodspy) values(@classid,@dicid,@goodname,@goodsnumber,@price,@createdate,@remark,@goodspy)";
            OleDbParameter[] rukupms = new OleDbParameter[]
                {
                    new OleDbParameter("@classid",c.ClassId),
                    new OleDbParameter("@dicid",c.DicId),
                    new OleDbParameter("@goodname",c.GoodName),
                    new OleDbParameter("@goodsnumber",c.Number),
                    new OleDbParameter("@price",c.Price),
                    new OleDbParameter("@createdate",c.Date.Date.ToString()),
                    new OleDbParameter("@remark",c.Remark),
                    new OleDbParameter("@goodspy",c.PY) 
                };

            //int num = new BllStock().GetById(c.DicId).Number;
            string stocksql;
            OleDbParameter[] stockpms;
            stocksql = "update stock set goodsnumber=goodsnumber-@goodsnumber where dicid=@dicid";
            stockpms = new OleDbParameter[]
                {
                    new OleDbParameter("@goodsnumber",c.Number),
                    new OleDbParameter("@dicid",c.DicId)
                };
            //历史库存
            int hbid = new BllHistoryStock().IsExistByDate(c.Date);
            if (hbid <= 0)
            {
                new BllHistoryStock().Insert(c.Date);
                //生成历史库存
                hbid = new BllHistoryStock().IsExistByDate(c.Date);
                GenerateHisStock(c.Date);
            }
            //string hbsql = "insert into historystock(createyear,createmonth) values(@year,@month)";
            //OleDbParameter[] hbpms = new OleDbParameter[]
            //    {
            //        new OleDbParameter("@year",r.Date.Year.ToString()),
            //        new OleDbParameter("@month",r.Date.Month.ToString()) 
            //    };

            int hisid = new BllHistoryStockDetail().IsExist(c.DicId, hbid);
            //string hissql = "update historystockdetail set totalout=totalout+@totalout where id=@id ";
            string hissql = "update historystockdetail set goodsnumber=goodsnumber-@totalout,totalout=totalout+@totalout where id=@id";
            OleDbParameter[] hispms = new OleDbParameter[]
                {
                    new OleDbParameter("@totalout",c.Number),
                    new OleDbParameter("@id",hisid)
                    
                };
            try
            {
                access.OpenTran();
                access.ExecuteNonQuery(rukusql, rukupms);
                access.ExecuteNonQuery(stocksql, stockpms);
                access.ExecuteNonQuery(hissql, hispms);
                access.CommitTran();
            }
            catch (Exception ex)
            {
                access.TranRollBack();
                throw ex;
            }
        }
        public void TuiKu(TuiKu tui)
        {
            AccessHelp access = new AccessHelp();
            Stock s = new Stock();
            string tuikusql = "insert into tuiku(chukuid,dicid,goodsnumber,createdate,goodspy,tuiremark) values(@chukuid,@dicid,@goodsnumber,@createdate,@goodspy,@remark)";
            OleDbParameter[] tuikupms = new OleDbParameter[]
                {
                   new OleDbParameter("@chukuid",tui.ChuKuId),
                   new OleDbParameter("@dicid",tui.DicId),
                   new OleDbParameter("@goodsnumber",tui.GoodsNumber),
                   new OleDbParameter("@createdate",tui.CreateDate),
                   new OleDbParameter("@goodspy",tui.GoodPy),
                   new OleDbParameter("@remark",tui.TuiRemark) 
                };

            string chukusql = "update chuku set tui=1 where id=@chukuid";
            OleDbParameter[] chukupms = new OleDbParameter[]
                {
                    new OleDbParameter("@chukuid",tui.ChuKuId) 
                };
            string stocksql;
            OleDbParameter[] stockpms;
            stocksql = "update stock set goodsnumber=goodsnumber+@goodsnumber where dicid=@dicid";
            stockpms = new OleDbParameter[]
                {
                    new OleDbParameter("@goodsnumber",tui.GoodsNumber),
                    new OleDbParameter("@dicid",tui.DicId)
                };
            //历史库存
            int hbid = new BllHistoryStock().IsExistByDate(tui.CreateDate);
            if (hbid <= 0)
            {
                new BllHistoryStock().Insert(tui.CreateDate);
                //生成历史库存
                hbid = new BllHistoryStock().IsExistByDate(tui.CreateDate);
                GenerateHisStock(tui.CreateDate);
            }
            int hisid = new BllHistoryStockDetail().IsExist(tui.DicId, hbid);
            string hissql = "update historystockdetail set goodsnumber=goodsnumber+@totalreturn,totalreturn=totalreturn+@totalreturn where id=@id";
            OleDbParameter[] hispms = new OleDbParameter[]
                {
                    new OleDbParameter("@totalreturn",tui.GoodsNumber),
                    new OleDbParameter("@id",hisid)
                    
                };
            try
            {
                access.OpenTran();
                access.ExecuteNonQuery(tuikusql, tuikupms);
                //access.ExecuteNonQuery(chukusql, chukupms);
                access.ExecuteNonQuery(stocksql, stockpms);
                access.ExecuteNonQuery(hissql, hispms);
                access.CommitTran();
            }
            catch (Exception ex)
            {
                access.TranRollBack();
                throw ex;
            }
        }
        public void qingkong()
        {
            string rsql = "delete from ruku";
            string csql = "delete from chuku";
            string tsql = "delete from tuiku";
            string hissql = "delete from historystock";
            string hisdetailsql = "delete from historystockdetail";
            string stocksql = "delete from stock";
            AccessHelp access = new AccessHelp();
            access.OpenTran();
            try
            {
                access.ExecuteNonQuery(rsql, null);
                access.ExecuteNonQuery(csql, null);
                access.ExecuteNonQuery(tsql, null);
                access.ExecuteNonQuery(hissql, null);
                access.ExecuteNonQuery(hisdetailsql, null);
                access.ExecuteNonQuery(stocksql, null);
                access.CommitTran();
            }
            catch (Exception ex)
            {
                access.TranRollBack();
                throw ex;
            }
        }
        public void GenerateHisStock(DateTime date)
        {
            List<Stock> stocks = new BllStock().GetAll();
            int hbid = new BllHistoryStock().IsExistByDate(date.AddMonths(-1));
            if (hbid <= 0)
            {
                new BllHistoryStock().Insert(date.Date);
                //生成历史库存
                hbid = new BllHistoryStock().IsExistByDate(date.AddMonths(-1));
            }
            foreach (Stock s in stocks)
            {
                int historydetailid = new BllHistoryStockDetail().IsExist(s.DicId, hbid);

                if (historydetailid <= 0)
                {
                    historystockdetail hsd = new historystockdetail();
                    hsd.BeginNumber = s.Number;
                    hsd.ClassId = s.ClassId;
                    hsd.DicId = s.DicId;
                    hsd.GoodsName = s.GoodsName;
                    hsd.GoodsNumber = s.Number;
                    hsd.HistoryId = hbid;
                    hsd.TotalIn = 0;
                    hsd.TotalOut = 0;
                    hsd.TotalReturn = 0;
                    hsd.Goodspy = s.PY;
                    new BllHistoryStockDetail().Insert(hsd);
                }
            }
        }        
        public void GenerateStartHisStock(DateTime date)
        {
                        
            int lasthbid = new BllHistoryStock().IsExistByDate(date.AddMonths(-1));
            //判断是否是期初第一个月
            if(lasthbid <=0)
            {
                int hbid = new BllHistoryStock().IsExistByDate(date);
                    if (hbid <= 0)
                    {
                        new BllHistoryStock().Insert(date);
                        //生成历史库存
                        hbid = new BllHistoryStock().IsExistByDate(date);
                    }
                    List<historystockdetail> hisstockdetails = new BllHistoryStockDetail().GetAllListByHisId(hbid);
                    foreach (historystockdetail hisdetails in hisstockdetails)
	                {
                        int ingoodsnum = 0;
                        int outgoodsnum = 0;
                        int returngoodsnum = 0;
                        List<RuKu> rlist = new BllRuKu().GetObjectByDicId(hisdetails.DicId,date);
                        List<ChuKu> clist = new BllChuKu().GetObjectByDicId(hisdetails.DicId,date);
                        List<TuiKu> tlist = new BllTuiKu().GetObjectByDicId(hisdetails.DicId,date); 
                        if (rlist.Count >0)
                        {
                            ingoodsnum = GetNumRuKu(rlist);
                            outgoodsnum = GetNumChuKu(clist);
                            returngoodsnum = GetNumChuKu(tlist);
                            hisdetails.BeginNumber = 0;
                            hisdetails.TotalIn = ingoodsnum;
                            hisdetails.TotalOut = outgoodsnum;
                            hisdetails.TotalReturn = returngoodsnum;
                            hisdetails.GoodsNumber = hisdetails.BeginNumber+ingoodsnum - outgoodsnum + returngoodsnum;
                            new BllHistoryStockDetail().Update(hisdetails);
                        }
                        else
	                    {
                            new BllHistoryStockDetail().Delete(hisdetails);
	                    }
                        //hisdetails.TotalIn = goodsnum;
                        //hisdetails.GoodsNumber = goodsnum;

		               
	                }
            }
            else
            {
                List<Stock> stocks = new BllStock().GetAll();
                int hbid = new BllHistoryStock().IsExistByDate(date);
                if (hbid <= 0) {
                    new BllHistoryStock().Insert(date.Date);
                    //生成历史库存
                    hbid = new BllHistoryStock().IsExistByDate(date);
                }
                foreach (Stock s in stocks) {
                    int historydetailid = new BllHistoryStockDetail().IsExist(s.DicId, hbid);
                    if (historydetailid <= 0) {
                        historystockdetail lastHisDetails = new BllHistoryStockDetail().GetObjectById(lasthbid);
                        int ingoodsnum = 0;
                        int outgoodsnum = 0;
                        int returngoodsnum = 0;
                        List<RuKu> rlist = new BllRuKu().GetObjectByDicId(s.DicId, date);
                        List<ChuKu> clist = new BllChuKu().GetObjectByDicId(s.DicId, date);
                        List<TuiKu> tlist = new BllTuiKu().GetObjectByDicId(s.DicId, date);
                        ingoodsnum = GetNumRuKu(rlist);
                        outgoodsnum = GetNumChuKu(clist);
                        returngoodsnum = GetNumChuKu(tlist);
                        historystockdetail hsd = new historystockdetail();
                        hsd.BeginNumber = lastHisDetails.GoodsNumber;
                        hsd.ClassId = s.ClassId;
                        hsd.DicId = s.DicId;
                        hsd.GoodsName = s.GoodsName;                        
                        hsd.HistoryId = hbid;
                        hsd.TotalIn = ingoodsnum;
                        hsd.TotalOut = outgoodsnum;
                        hsd.TotalReturn = returngoodsnum;
                        hsd.GoodsNumber = hsd.BeginNumber + ingoodsnum - outgoodsnum + returngoodsnum;
                        hsd.Goodspy = s.PY;
                        new BllHistoryStockDetail().Insert(hsd);
                    }
                    else 
                    {


                            int lasthistorydetailid = new BllHistoryStockDetail().IsExist(s.DicId, lasthbid);
                            historystockdetail hisdetails = new BllHistoryStockDetail().GetObjectById(historydetailid);
                            historystockdetail lastHisDetails = new BllHistoryStockDetail().GetObjectById(lasthistorydetailid);
                            int ingoodsnum = 0;
                            int outgoodsnum = 0;
                            int returngoodsnum = 0;
                            List<RuKu> rlist = new BllRuKu().GetObjectByDicId(hisdetails.DicId, date);
                            List<ChuKu> clist = new BllChuKu().GetObjectByDicId(hisdetails.DicId, date);
                            List<TuiKu> tlist = new BllTuiKu().GetObjectByDicId(hisdetails.DicId, date);                            
                            ingoodsnum = GetNumRuKu(rlist);
                            outgoodsnum = GetNumChuKu(clist);
                            returngoodsnum = GetNumChuKu(tlist);
                           
                            hisdetails.BeginNumber = lastHisDetails.GoodsNumber;
                            hisdetails.TotalIn = ingoodsnum;
                            hisdetails.TotalOut = outgoodsnum;
                            hisdetails.TotalReturn = returngoodsnum;
                            hisdetails.GoodsNumber = hisdetails.BeginNumber+ingoodsnum - outgoodsnum + returngoodsnum;
                            new BllHistoryStockDetail().Update(hisdetails);                        
                            //hisdetails.TotalIn = goodsnum;
                            //hisdetails.GoodsNumber = goodsnum;

                    }
                }
            }
            
        }

        private int GetNumChuKu(List<TuiKu> tlist)
        {
            int totalNum = 0;
            foreach (TuiKu item in tlist)
            {
                totalNum = totalNum + item.GoodsNumber;
            }
            return totalNum;
        }

        private int GetNumChuKu(List<ChuKu> clist)
        {
            int totalNum = 0;
            foreach (ChuKu item in clist)
            {
                totalNum = totalNum + item.Number;
            }
            return totalNum;
        }
        public int GetNumRuKu(List<RuKu> rlist)
        {
            int goodsnum = 0;
            foreach (RuKu r in rlist)
            {
                goodsnum = goodsnum + r.Number;
            }
            return goodsnum;
        }
       
    }
}
